package com.readbook.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("all")
public class PayCommonUtil {
    //优生活
    private static Logger log = LoggerFactory.getLogger(PayCommonUtil.class);
    private static String shdlzh = "1488584142@1488584142";//商户平台登录账号
    private static String zfshh = "1488584142";//商户商户
    private static String shdlma = "510183";//商户平台登录密码
    private static String key = "wx36ddd973cfeb31eccai51018314885";//商户支付秘钥
//	1714302454@qq.com  680324mmy 环西小学小程序邮箱和密码
//	2327985064@qq.com 密码 immortal517  微信公公众号
//	oUK0S0VnWlDl6YgzuatXKI-k4SJU //管理员的oppenid
//	wx0776fc402b828b49  微信小程序appid
//	73d3e290d0d430abbda2f9c0f56be7ed 小程序秘钥
//公众号 ：wx36ddd973cfeb31ec	 秘钥：b4667459e60067f0ce0da927e2356353

    //腾讯云账号：1404704175@qq.com  Saving!.!0517  Saving!.!0517

    public static String CreateNoncestr(int length) {
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        String res = "";
        for (int i = 0; i < length; i++) {
            Random rd = new Random();
            res += chars.indexOf(rd.nextInt(chars.length() - 1));
        }
        return res;
    }

    public static String CreateNoncestr() {
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        String res = "";
        for (int i = 0; i < 16; i++) {
            Random rd = new Random();
            res += chars.charAt(rd.nextInt(chars.length() - 1));
        }
        return res;
    }

    /**
     * @param characterEncoding �����ʽ
     * @param params            �������
     * @return
     * @Description��signǩ��
     */
    public static String createSign(SortedMap<String, Object> params, ConfKit confkit) {
        StringBuffer sb = new StringBuffer();
        Set es = params.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k)
                    && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + confkit.get("key"));
        String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
        return sign;
    }

    /**
     * @param parameters�������
     * @return
     * @Description�����������ת��Ϊxml��ʽ��string
     */
    public static String getRequestXml(SortedMap<String, Object> parameters) {
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k) || "sign".equalsIgnoreCase(k) || "detail".equalsIgnoreCase(k)) {
                sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
            } else {
                sb.append("<" + k + ">" + v + "</" + k + ">");
            }
        }
        sb.append("</xml>");
        return sb.toString();
    }

    /**
     * @param return_code ���ر���
     * @param return_msg  ������Ϣ
     * @return
     * @Description�����ظ�΢�ŵĲ���
     */
    public static String setXML(String return_code, String return_msg) {
        return "<xml><return_code><![CDATA[" + return_code
                + "]]></return_code><return_msg><![CDATA[" + return_msg
                + "]]></return_msg></xml>";
    }

    public static String setNativeXML(SortedMap<Object, Object> parameters) {
        return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[]]></return_msg><product_id><![CDATA[" + parameters.get("prepayid") + "]]></product_id></xml>";
    }

    /**
     * @param characterEncoding �����ʽ
     * @param params            �������
     * @return
     * @Description��signǩ��
     */
    public static String createPaySign(SortedMap<String, Object> params) {
        StringBuffer sb = new StringBuffer();
        Set es = params.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k)
                    && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        System.out.println("paysb:" + sb);
        String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
        System.out.println("paysign:" + sign);
        return sign;
    }
}

